﻿// Handles notifications of external mouse clicks
define([
    'Wjs/ui/elements/notifiers/mouseupNotifier'
],
function () {
    'use strict';

    var mouseupNotifier = Wjs.getType('mouseupNotifier'),
        ExternalMouseUpHandler = Wjs.defineOnce({

            type: 'ExternalMouseUpHandler',

            internal: {

                construct: function (cfg, parent) {

                    this.listenOnExternalMouseup = cfg.listenOnExternalMouseup || this.listenOnExternalMouseup;
                    this.onExternalMouseup = cfg.onExternalMouseup || this.onExternalMouseup;

                    if (this.listenOnExternalMouseup) { // If this view wants to get notified of an external click

                        mouseupNotifier.subscribe(this);
                    }
                },

                listeners: {

                    // Called when the element is about to be destroyed
                    onDestroy: function () {

                        if (this.listenOnExternalMouseup) {

                            mouseupNotifier.unSubscribe(this);
                        }

                        this.listenOnExternalMouseup = null;
                    }
                }

            }

        });

    return ExternalMouseUpHandler;
});